package com.zhuangfei.hpuhelper.view;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hpu.constants.ShareConstants;
import com.hpu.constants.WebConstants;
import com.hpu.tools.ShareTools;
import com.tencent.bugly.beta.Beta;
import com.tencent.connect.common.Constants;
import com.tencent.connect.share.QQShare;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;
import com.uuzuche.lib_zxing.activity.CodeUtils;
import com.zhuangfei.hpuhelper.tools.ReflectTools;
import com.zhuangfei.toolkit.IntentToolkit;
import com.zhuangfei.toolkit.model.BundleModel;
import com.zhuangfei.toolkit.tools.ActivityTools;
import com.zhuangfei.toolkit.tools.BundleTools;
import com.zhuangfei.toolkit.tools.ToastTools;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnItemClick;
import cn.lzf.serviceplatform.R;

import static android.provider.UserDictionary.Words.APP_ID;

public class MenuActivity extends AppCompatActivity {

    Activity context;

    boolean isHidden = true;

    private final static int REQUEST_CODE = 1;

    Tencent mTencent;

    MyIUiListener mIUiListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);
        ActivityTools.setTransparent(this);
        ButterKnife.bind(this);
        // Tencent类是SDK的主要实现类，开发者可通过Tencent类访问腾讯开放的OpenAPI。
// 其中APP_ID是分配给第三方应用的appid，类型为String。
        mIUiListener=new MyIUiListener();
        mTencent = Tencent.createInstance("3103430697",getApplicationContext());
        inits();
    }

    public void bug() {
        String str = null;
        ToastTools.show(this, "这里有BUG");
        Log.i("Str", str.length() + "");
    }

    private void inits() {
        context = this;
        String number = (String) ShareTools.get(MenuActivity.this, ShareConstants.STORE_USER, "未知学号");
    }

    public Activity getContext() {
        return context;
    }

    @OnClick(R.id.id_menu_clear)
    public void clearData() {
        ShareTools.clear(this);
        Intent intent = new Intent(context, LoadActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                Intent.FLAG_ACTIVITY_CLEAR_TASK);
        context.startActivity(intent);
        context.overridePendingTransition(com.zhuangfei.toolkit.R.anim.slide_in, com.zhuangfei.toolkit.R.anim.slide_out);//动画
        context.finish();
    }

    @OnClick(R.id.id_menu_notime)
    public void onNoTimeLayoutClick() {
        new IntentToolkit().with(this)
                .target(NoTimeClassActivity.class)
                .ignoreFinish()
                .execute();
    }

    @OnClick(R.id.id_menu_person)
    public void onPersonLayoutClick() {
        new IntentToolkit().with(this)
                .target(PersonActivity.class)
                .ignoreFinish()
                .execute();
    }

    @OnClick(R.id.id_back)
    public void goBack() {
        ActivityTools.backActivity(getContext(), MainActivity.class);
    }

    @OnClick(R.id.id_menu_issues)
    public void toSubmitIssues() {
        showNormalDialog();
    }

    private void showNormalDialog() {
        if(MenuActivity.this==null) return;
        AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(MenuActivity.this);
        normalDialog.setTitle("问题反馈");
        normalDialog.setMessage("您的意见或问题我们会尽快解决\n\n请加群：617094684\n\n或联系QQ：1193600556");
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });

        normalDialog.create().show();
    }

    @Override
    public void onBackPressed() {
        goBack();
    }

    @OnClick(R.id.id_menu_score_layout)
    public void onScoreLayoutClick() {
        ActivityTools.toActivity(this, ScoreTotalActivity.class);
    }

    @OnClick(R.id.id_menu_news_layout)
    public void onNewsLayoutClick() {
        ActivityTools.toActivity(this, NewsTotalActivity.class);
    }

    @OnClick(R.id.id_menu_repair_layout)
    public void onRepairTextViewClick() {
        BundleModel model = new BundleModel();
        model.setFromClass(MenuActivity.class);
        model.put("title", ReflectTools.TITLE_REPAIR);
        model.put("url", ReflectTools.URL_REPAIR);
        ActivityTools.toActivity(this, WebViewActivity.class, model);
    }

    @OnClick(R.id.id_menu_lecture_layout)
    public void onLectureTextViewClick() {
        new IntentToolkit().with(this)
                .target(LectureActivity.class)
                .ignoreFinish()
                .execute();
    }


    @OnClick(R.id.id_menu_repair2_layout)
    public void onRepairTextView2Click() {
        BundleModel model = new BundleModel();
        model.setFromClass(MenuActivity.class);
        model.put("title", ReflectTools.TITLE_REPAIR2);
        model.put("url", ReflectTools.URL_REPAIR2);
        ActivityTools.toActivity(this, WebViewActivity.class, model);
    }

    @OnClick(R.id.id_menu_libary_layout)
    public void onLibaryLayoutClick() {
        ActivityTools.toActivity(this, SearchBookActivity.class);
    }

    @OnClick(R.id.id_menu_emptyroom_layout)
    public void onEmptyRoomLayoutClick() {
        ActivityTools.toActivity(this, EmptyRoomActivity.class);
    }

    @OnClick(R.id.id_menu_optionclass_layout)
    public void onOptionClassLayoutClick() {
        ActivityTools.toActivity(this, OptionalClassActivity.class);
    }

    @OnClick(R.id.id_menu_saike_layout)
    public void onSaikeLayoutClick() {
        BundleModel model = new BundleModel();
        model.setFromClass(MenuActivity.class);
        model.put("title", ReflectTools.TITLE_SAIKE);
        model.put("url", ReflectTools.URL_SAIKE);
        ActivityTools.toActivity(this, WebViewActivity.class, model);
    }

    @OnClick(R.id.id_menu_manager)
    public void onManagerLayoutClick() {
        ActivityTools.toActivity(this, CourseManagerActivity.class);
    }

    @OnClick(R.id.id_menu_scan)
    public void onScanLayoutClick() {
        ActivityTools.toActivity(this, ScanActivity.class);
    }

    @OnClick(R.id.id_menu_electric_layout)
    public void onElectricLayoutClick() {
        ActivityTools.toActivity(this, ElectricActivity.class);
    }


    @OnClick(R.id.id_menu_update)
    public void onUpdateLayoutClick() {
        Beta.checkUpgrade();
    }


    @OnClick(R.id.id_menu_share)
    public void shareToQQ() {
        ToastTools.show(this,"正在拉取列表，请稍侯..");
        Bundle params = new Bundle();
        params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
        params.putString(QQShare.SHARE_TO_QQ_TITLE, "HPU校园");// 标题
        params.putString(QQShare.SHARE_TO_QQ_SUMMARY, "一款便捷的河南理工大学校园软件");// 摘要
        params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, "http://www.hpuxiaoyuan.com/hpu/index.php?p=front&c=PublicView&a=showAppShare");// 内容地址

        params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, "https://hpuxiaoyuan.com/hpu/logo.png");// 网络图片地址
        params.putString(QQShare.SHARE_TO_QQ_APP_NAME, "HPU校园");// 应用名称
        params.putString(QQShare.SHARE_TO_QQ_EXT_INT, "来自HPU校园");
// 分享操作要在主线程中完成
        mTencent.shareToQQ(MenuActivity.this, params, mIUiListener);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Tencent.onActivityResultData(requestCode, resultCode, data, mIUiListener);
        if (requestCode == Constants.REQUEST_API) {
            if (resultCode == Constants.REQUEST_QQ_SHARE || resultCode == Constants.REQUEST_QZONE_SHARE || resultCode == Constants.REQUEST_OLD_SHARE) {
                Tencent.handleResultData(data, mIUiListener);
            }
        }
    }

    class MyIUiListener implements IUiListener {
        @Override
        public void onComplete(Object o) {
        }

        @Override
        public void onError(UiError uiError) {
        }

        @Override
        public void onCancel() {
        }

    }
}